-- gui_panel.lua

gui_panel = class('gui_panel', gui_element)

function gui_panel:initialize()
	self.pos = {x = 0, y = 0}
	self.size = {w = 0, h = 0}
	self.background = {r = 255, g = 255, b = 255}
	self.border = {r = 255, g = 255, b = 255}
	self.titlebartextcolour = {r = 0, g = 0, b = 0}
	self.has_titlebar = true
	self.string = "Window"
	self.name = "Panel"
	self.grabbed = false
	self.gui_elements = {}
	self.grabbable = true
	
	self.theme = "dark"
	
	self.checkbox ={blank = love.graphics.newImage("gui_library/themes/"..self.theme.."/checkbox_blank.png"),
					checked = love.graphics.newImage("gui_library/themes/"..self.theme.."/checkbox_checked.png"),
					hover = love.graphics.newImage("gui_library/themes/"..self.theme.."/checkbox_hover.png")}
end

function gui_panel:draw()
	love.graphics.setColor(self.background.r, self.background.g, self.background.b)
	love.graphics.rectangle("fill", self.pos.x, self.pos.y, self.size.w, self.size.h)
	
	love.graphics.setColor(self.border.r, self.border.g, self.border.b)
	love.graphics.rectangle("line", self.pos.x - 1, self.pos.y - 1, self.size.w + 1, self.size.h + 1)
	
	if self.has_titlebar then
		-- Titlebar
		love.graphics.setColor(self.border.r, self.border.g, self.border.b)
		love.graphics.rectangle("fill", self.pos.x - 2, self.pos.y - 20, self.size.w + 2, 20)
		-- Titlebar text
		love.graphics.setColor(self.titlebartextcolour.r, self.titlebartextcolour.g, self.titlebartextcolour.b)
		love.graphics.print(self.string, self.pos.x + 5, self.pos.y - 18)
	end
	
	love.graphics.setColor(255, 255, 255)
	
	for i, v in ipairs(self.gui_elements) do
		if v.visible then
			v:draw()
		end
	end
end

function gui_panel:update(dt)
	for i, v in ipairs(self.gui_elements) do
		if v.name == "Checkbox" then
			v.graphics.blank = self.checkbox.blank
			v.graphics.checked = self.checkbox.checked
			v.graphics.hover = self.checkbox.hover
		end
	
		v:update(dt)
	end
	
	if CheckCollision(self.pos.x, self.pos.y - 20, self.size.w, 20, love.mouse.getX(), love.mouse.getY(), 1, 1) then
		if love.mouse.isDown("l") then
			self.grabbed = true
		else
			self.grabbed = false
		end
	end
	
	if self.grabbed then
		local x_offset = self.pos.x - love.mouse.getX()
		local y_offset = self.pos.y - love.mouse.getY()
		self.pos.x = love.mouse.getX() - self.size.w / 2
		self.pos.y = love.mouse.getY() + 10
		
		for i, v in ipairs(self.gui_elements) do
			v.panel_x = self.pos.x
			v.panel_y = self.pos.y
		end
	end
end

function gui_panel:mousepressed(x, y, button)
	for i, v in ipairs(self.gui_elements) do
		if v.visible then
			v:mousepressed(x, y, button)
		end
	end
end

function gui_panel:mousereleased(x, y, button)
	for i, v in ipairs(self.gui_elements) do
		if v.visible then
			v:mousereleased(x, y, button)
		end
	end
end

function gui_panel:set_titlebar_text_colour(toR, toG, toB)
	self.titlebartextcolour = {r = toR, g = toG, b = toB}
end

-- Adding elements
function gui_panel:add_element(toAdd)
	print("Adding "..toAdd.name.." to '"..self.string.."' window.")
	toAdd.panel_x = self.pos.x
	toAdd.panel_y = self.pos.y
	table.insert(self.gui_elements, toAdd)
end